﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Timers;
using SpeechLib;

namespace typewriter {
    class twTalk : twPlugin {
        public Label statusMsg;

        public widge x;
        public Button doitall;

        private SpeechLib.SpSharedRecoContext objRecoContext = null;
        private SpeechLib.ISpeechRecoGrammar grammar = null;
        private SpeechLib.ISpeechGrammarRule menuRule = null;
        bool needload = true;

        public twTalk() {
            this.name = "Voice Commands";
            this.author = "WillWharton.com";
            this.desc = "Allows you to dictate commands";
            this.ver = "0.1a";


            x = new widge(200, 50);
            
            
            tw.SetTopmost(x.Handle, true);

            doitall = new Button();
            doitall.Text = "Load";            
            doitall.Location = new System.Drawing.Point(80, 20);
            doitall.Click += new EventHandler(doitall_Click);
            doitall.BackColor = Color.Blue;
            doitall.ForeColor = Color.White;
            doitall.FlatAppearance.BorderSize = 2;
            x.Controls.Add(doitall);



            statusMsg = new Label();  
            statusMsg.BackColor = Color.Transparent;
            statusMsg.ForeColor = Color.White;
            statusMsg.AutoSize = true;
            statusMsg.Location = new System.Drawing.Point(10, 8);
            statusMsg.MouseDown += new System.Windows.Forms.MouseEventHandler(statusMsg_MouseDown);
            x.Controls.Add(statusMsg);
            
            setMsg("Not Ready!");
            
            x.Show();
            x.setPos(new Point(600, 0));
           // x.DoubleClick += new EventHandler(statusMsg_DoubleClick);
            /*
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(53, 32);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
             */





        }

        void doitall_Click(object sender, EventArgs e) {
            if (needload) {

                setMsg("Initializing Speech Engine....");
                // Get an insance of RecoContext. I am using the shared RecoContext.
                objRecoContext = new SpeechLib.SpSharedRecoContext();
                // Assign a eventhandler for the Hypothesis Event.
                objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event);
                // Assign a eventhandler for the Recognition Event.
                objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);

                grammar = objRecoContext.CreateGrammar(0);
			
                menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                object PropValue = "";

                menuRule.InitialState.AddWordTransition(null, "will", " ", SpeechGrammarWordType.SGLexical, "will", 1, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "New", " ", SpeechGrammarWordType.SGLexical, "New", 1, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Open", " ", SpeechGrammarWordType.SGLexical, "Open", 2, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Close", " ", SpeechGrammarWordType.SGLexical, "Close", 3, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Exit", " ", SpeechGrammarWordType.SGLexical, "Exit", 4, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Cut", " ", SpeechGrammarWordType.SGLexical, "Cut", 5, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Copy", " ", SpeechGrammarWordType.SGLexical, "Copy", 6, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Paste", " ", SpeechGrammarWordType.SGLexical, "Paste", 7, ref PropValue, 1.0F);
                menuRule.InitialState.AddWordTransition(null, "Delete", " ", SpeechGrammarWordType.SGLexical, "Delete", 8, ref PropValue, 1.0F);


                grammar.Rules.Commit();
                grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
                setMsg("Speech Engine Ready for Input");

                needload = false;


            }
        }

        private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result) {
           insertWord("R:"+ Result.PhraseInfo.GetText(0, -1, true)+" ");
        }

        private void Hypo_Event(int StreamNumber, object StreamPosition, ISpeechRecoResult Result) {
            insertWord("H:" + Result.PhraseInfo.GetText(0, -1, true) + " ");

        }


        public void insertWord(string msg) {
            if (tw.active != null) {
                tw.active.rtb.Text += msg + "\n";
            }
        }



        public void setMsg(string msg) {
            statusMsg.Text = "Voice Command\n"+msg;
        }

        public override void tick() {
            //this.thetime.Text = "XX";
            
        }

        private void statusMsg_MouseDown(object sender, MouseEventArgs e) {
            x.widge_MD(sender, e);
        }
    }
}

